package reflect;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 使用反射机制进行对象实例化操作
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Person p = new Person();
        System.out.println(p);

        //1先加载对应的类对象
//        Class cls = Class.forName("reflect.Person");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个类的完全限定名:");
        String className = scanner.nextLine();
        Class cls = Class.forName(className);

        //2通过类对象的newInstance()方法可以相当与调用该类的公开的无参构造器实例化
        Object obj = cls.newInstance();
        System.out.println(obj);
    }
}






